package com.ellecity06.common.ktx

import android.annotation.SuppressLint
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.ellecity06.common.R

/**
 * @author ellecity06
 * @time 2024/10/23 15:34
 * @des
 */
@SuppressLint("CheckResult")
fun ImageView.load(context: Context, url: String?,radius:Float?=null, opt: RequestOptions? = null) {
    val options: RequestOptions = opt ?: RequestOptions().apply {
//        placeholder(R.mipmap.placeholder_good)
//        error(R.mipmap.placeholder_good)
    }
    if (radius!=null)
        options.transform( CenterCrop(),  RoundedCorners(radius.dp2px()))
    Glide.with(context)
        .load(url)
        .apply(options)
        .into(this)
}

@SuppressLint("CheckResult")
fun ImageView.loadByUser(context: Context, url: String?,radius:Float?=null, opt: RequestOptions? = null) {
    val options: RequestOptions = opt ?: RequestOptions().apply {
        placeholder(R.mipmap.placeholder_user)
        error(R.mipmap.placeholder_user)
    }
    if (radius!=null)
        options.transform( CenterCrop(),  RoundedCorners(radius.dp2px()))
    Glide.with(context)
        .load(url)
        .apply(options)
        .into(this)
}

@SuppressLint("CheckResult")
fun ImageView.loadGif(context: Context, url: Any?, opt: RequestOptions? = null) {
    val options: RequestOptions = opt ?: RequestOptions().apply {
//        placeholder(R.mipmap.placeholder_user)
//        error(R.mipmap.placeholder_user)
    }
    Glide.with(context).asGif()
        .load(url)
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .apply(options)
        .into(this)
}

